// sgllib.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "sgllib.h"
#include "sglinfo.h"
#include "sglimage.h"
#include "sglframe.h"
#include "gdirender.h"
#include "ddrawrender.h"
#include "d3drender.h"


SGLLIB_API INT SGLGetInfo(PSGLINFO *ppInfo, LPCTSTR pszFile)
{
	if (!ppInfo)
		return SGL_NULLPOINTER;

	*ppInfo = NULL;

	if (!pszFile)
		return SGL_NULLPOINTER;

	SglInfo *pInfo = new SglInfo;
	if (!pInfo)
		return SGL_OUTOFMEMORY;

	if (!pInfo->Open(pszFile))
	{
		INT nRet = pInfo->GetError();
		delete pInfo;
		return nRet;
	}

	*ppInfo = pInfo->GetHandle();
	return SGL_SUCCESS;
}

SGLLIB_API INT SGLReleaseInfo(PSGLINFO pInfo)
{
	if (!pInfo)
		return SGL_NULLPOINTER;

	SglInfo *pInf = SglInfo::FromHandle(pInfo);
	INT nRet = SGL_SUCCESS;
	if (!pInf->Close())
		nRet = pInf->GetError();

	delete pInf;
	return nRet;
}

SGLLIB_API INT SGLGetImage(PSGLIMG *ppImage, LPCTSTR pszFile, UINT nSubIdx)
{
	if (!ppImage)
		return SGL_NULLPOINTER;

	*ppImage = NULL;

	if (!pszFile)
		return SGL_NULLPOINTER;

	SglImage *pImage = new SglImage;
	if (!pImage)
		return SGL_OUTOFMEMORY;

	if (!pImage->Open(pszFile, nSubIdx))
	{
		INT nRet = pImage->GetError();
		delete pImage;
		return nRet;
	}

	*ppImage = pImage->GetHandle();
	return SGL_SUCCESS;
}

SGLLIB_API INT SGLReleaseImage(PSGLIMG pImage)
{
	if (!pImage)
		return SGL_NULLPOINTER;

	SglImage *pImg = SglImage::FromHandle(pImage);
	INT nRet = SGL_SUCCESS;
	if (!pImg->Close())
		nRet = pImg->GetError();

	delete pImg;
	return nRet;
}

SGLLIB_API INT SGLDecode(HANDLE hFrame)
{
	if (!hFrame)
		return SGL_NULLPOINTER;

	SglFrame *pFrame = static_cast<SglFrame *>(hFrame);
	if (!pFrame->Decode())
		return pFrame->GetError();

	return SGL_SUCCESS;
}

SGLLIB_API INT SGLRelease(HANDLE hFrame)
{
	if (!hFrame)
		return SGL_NULLPOINTER;

	SglFrame *pFrame = static_cast<SglFrame *>(hFrame);
	if (!pFrame->Release())
		return pFrame->GetError();

	return SGL_SUCCESS;
}

SGLLIB_API INT SGLGetRender(PHANDLE pRender, INT nRenderType, HWND hWnd, INT nWidth, INT nHeight, BOOL bFullScreen)
{
	if (!pRender || !hWnd)
		return SGL_NULLPOINTER;

	*pRender = NULL;
	SglRender *pHandle = NULL;

	switch (nRenderType)
	{
	case SGL_RENDER_GDI:
		pHandle = new GdiRender;
		break;
	case SGL_RENDER_DIRECTDRAW:
		pHandle = new DDrawRender;
		break;
	case SGL_RENDER_DIRECT3D:
		pHandle = new D3DRender;
		break;
	default:
		return SGL_RENDERTYPEINVALID;
	}

	if (!pHandle)
		return SGL_OUTOFMEMORY;

	if (!pHandle->Create(hWnd, nWidth, nHeight, bFullScreen))
	{
		INT nRet = pHandle->GetError();
		delete pHandle;
		return nRet;
	}

	*pRender = pHandle;
	return SGL_SUCCESS;
}


SGLLIB_API INT SGLBeginDraw(HANDLE hRender)
{
	if (!hRender)
		return SGL_NULLPOINTER;

	SglRender *pRender = static_cast<SglRender *>(hRender);

	if (!pRender->BeginDraw())
		return pRender->GetError();

	return SGL_SUCCESS;
}

SGLLIB_API INT SGLDraw(HANDLE hRender, HANDLE hFrame, COLORREF dwBgColor)
{
	if (!hRender || !hFrame)
		return SGL_NULLPOINTER;

	SglFrame *pFrame = static_cast<SglFrame *>(hFrame);
	const SglFrame::BUFFER *pBuffer = pFrame->GetBuffer();
	if (!pBuffer)
		return pFrame->GetError();

	SglRender *pRender = static_cast<SglRender *>(hRender);
	if (!pRender->Draw(pBuffer, dwBgColor))
		return pRender->GetError();

	return SGL_SUCCESS;
}


SGLLIB_API INT SGLEndDraw(HANDLE hRender)
{
	if (!hRender)
		return SGL_NULLPOINTER;

	SglRender *pRender = static_cast<SglRender *>(hRender);

	if (!pRender->EndDraw())
		return pRender->GetError();

	return SGL_SUCCESS;

}

SGLLIB_API INT SGLDrawPixel(HANDLE hRender, INT nX, INT nY, COLORREF dwColor, BYTE byAlpha)
{
	if (!hRender)
		return SGL_NULLPOINTER;

	SglRender *pRender = static_cast<SglRender *>(hRender);
	if (!pRender->DrawPixel(nX, nY, dwColor, byAlpha))
		return pRender->GetError();

	return SGL_SUCCESS;
}

SGLLIB_API INT SGLRender(HANDLE hRender, LPCRECT pRect)
{
	if (!hRender)
		return SGL_NULLPOINTER;

	SglRender *pRender = static_cast<SglRender *>(hRender);
	if (!pRender->Render(pRect))
		return pRender->GetError();

	return SGL_SUCCESS;
}

SGLLIB_API INT SGLReleaseRender(HANDLE hRender)
{
	if (!hRender)
		return SGL_NULLPOINTER;

	SglRender *pRender = static_cast<SglRender *>(hRender);
	pRender->Destroy();
	delete pRender;
	return SGL_SUCCESS;
}
